package com.huike.web.controller.business;

import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.vo.TbBusinessTrackRecordVo;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: qingtao
 * @Date 2022/3/31 16:48
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController {
    @Autowired
    private ITbBusinessTrackRecordService iTbBusinessTrackRecordService;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    @GetMapping("/list")
    public AjaxResult list(Long businessId){
        List<TbBusinessTrackRecord> list = iTbBusinessTrackRecordService.list(businessId);
        for (TbBusinessTrackRecord trackRecord : list) {
            String[] items = trackRecord.getKeyItems().split(",");
            for (String item : items) {
                String dictLabel = iSysDictDataService.selectDictLabel("communication_point", item);
                trackRecord.getKeys().add(dictLabel);
            }
        }
        return AjaxResult.success(list);
    }


    /**
     *添加商机跟进记录
     * @param
     * @return
     */
    @PostMapping
    public AjaxResult listAll(@RequestBody TbBusinessTrackRecordVo tbBusinessTrackRecordVo){

        TbBusinessTrackRecord businessRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(tbBusinessTrackRecordVo,businessRecord);

        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(tbBusinessTrackRecordVo,tbBusiness);

        iTbBusinessTrackRecordService.saveBusinessTrackRecordService(businessRecord,tbBusiness);
        return AjaxResult.success();
    }
}
